package com.canbot.u05.activity.training;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.activity.shoppingGuide.sales.TextToSpeechActivity;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ConstUtils;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.view.UsherModeRecordDialog;

import java.io.File;
import java.util.HashMap;

/**
 * 培训主持模式------音频设置界面
 */

public class TrainingAudioSettingActivity extends BaseActivityNoTitle implements View.OnClickListener {

        private ImageView imageViewAudio;

        private ImageView imageViewText;

        private ImageView imageViewRecord;

        private String isUpdate;

        private String path;

        private String actionId;

        private String mPicPath;

        private String taskName;

        private boolean isTTS = false;

        private String ttsText;

        private boolean isAudioFile;

        private UsherModeRecordDialog usherModeRecordDialog;

        private String fileName;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_training_host_audio_setting);
                initView();
                isUpdate = getIntent().getStringExtra("isUpdate");
                mPicPath=getIntent().getStringExtra("mPicPath");
                taskName=getIntent().getStringExtra("taskName");
                isAudioFile = getIntent().getBooleanExtra("isAudioFile",false);

                if (!TextUtils.isEmpty(isUpdate)) {
                        if (isAudioFile) {
                                imageViewAudio.setVisibility(View.VISIBLE);
                                path = getIntent().getStringExtra("oldPath");
                        }
                        else {
                                imageViewText.setVisibility(View.VISIBLE);
                                ttsText = getIntent().getStringExtra("oldPath");
                        }
                }
                if(!TextUtils.isEmpty(getIntent().getStringExtra("isFirst")) && TextUtils.isEmpty(isUpdate)){
                        MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.TRAINING_DIR + "rwbzbzts_pxzc_u05_002.wav");
                        MsgSendUtils.sendStringMsg(MsgType.EYE_MOTION, "26");
                }
        }

        @Override
        protected void onResume() {
                super.onResume();
        }

        private void initView() {
                findViewById(R.id.rl_audio).setOnClickListener(this);
                findViewById(R.id.rl_text_to_speech).setOnClickListener(this);
                findViewById(R.id.rl_record).setOnClickListener(this);
                findViewById(R.id.bt_save).setOnClickListener(this);
                findViewById(R.id.bt_back).setOnClickListener(this);

                imageViewAudio = (ImageView) findViewById(R.id.cbox_choose_exist_audio);
                imageViewText = (ImageView) findViewById(R.id.cbox_choose_text_to_speech);
                imageViewRecord = (ImageView) findViewById(R.id.cbox_choose_record);
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.bt_back:
                                if (TextUtils.isEmpty(isUpdate)) {
                                        Intent intent = new Intent();
                                        intent.putExtra("actionId", actionId);
                                        intent.putExtra("path", path);
                                        setResult(RESULT_OK, intent);
                                }
                                finish();
                                break;
                        case R.id.bt_save:

                                if (!TextUtils.isEmpty(isUpdate)) {
                                        setResult(RESULT_OK, new Intent()
                                                .putExtra("path", path)
                                                .putExtra("isTts", isTTS));

                                        finish();
                                        return;
                                }
                                if (TextUtils.isEmpty(path)) {
                                        ToastTools.newInstance(this).showText(getString(R.string.voice_cannot_empty));
                                        return;
                                }
                                Intent intent = new Intent(TrainingAudioSettingActivity.this, TrainingActionSettingActivity.class);
                                intent.putExtra("mPicPath", mPicPath);
                                intent.putExtra("path", path);
                                intent.putExtra("isTTS", isTTS);
                                intent.putExtra("taskName", taskName);
                                intent.putExtra("isFirst", getIntent().getStringExtra("isFirst"));
                                startActivityForResult(intent, 111);
                                overridePendingTransition(R.anim.face_zoom_enter,
                                        R.anim.face_zoom_exit);
                                break;
                        case R.id.record_dialog_finish:
                                fileName = usherModeRecordDialog.ok();
                                usherModeRecordDialog.setTime(TrainingAudioSettingActivity.this.getString(R.string.uploading_please_wait));
                                updataFile();
                                break;
                        case R.id.record_dialog_cancle:
                                finish();
                                usherModeRecordDialog.no();
                                break;
                        case R.id.rl_record:
                                showRecord();
                                break;
                        case R.id.rl_text_to_speech:
                                intent = new Intent(TrainingAudioSettingActivity.this, TextToSpeechActivity.class);
                                intent.putExtra("voice", ttsText);
                                startActivityForResult(intent, 102);
                                break;
                        case R.id.rl_audio:
                                intent = new Intent(TrainingAudioSettingActivity.this, TrainingChooseAudioActivity.class);
                                intent.putExtra("mPicPath", path);
                                startActivityForResult(intent, 101);
                                break;
                }
        }


        private void showRecord() {
                if (usherModeRecordDialog == null) {
                        //点击语音录入图标
                        usherModeRecordDialog = new UsherModeRecordDialog(this, UsherModeRecordDialog.HOST_MODE);
                }
                usherModeRecordDialog.setFinishClickListener(this);
                usherModeRecordDialog.setCancleClickListener(this);
                setDialogWindowAttr(usherModeRecordDialog, this);
                usherModeRecordDialog.show();
        }

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x848);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x499);
                dlg.getWindow().setAttributes(lp);
        }

        private void updataFile() {

                HashMap<String, Object> hasMap = new HashMap<>();

                File file = new File(ConstUtils.PATH_SD + "/record/", fileName);
                if (!file.exists()) {
                        file.mkdirs();
                }

                hasMap.put("path", fileName);
                hasMap.put("audiofile", file);

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "uploadRecord", hasMap, new OkHttpUtils.OkHttpCallBack() {

                        @Override
                        public void onSuccess(String result) {

                                final JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                Logger.e("save", "onSuccess:" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/" + "strMsgBean.getRc()" + strMsgBean.getRc());
                                if (strMsgBean.getRc() == 0) {
                                        runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                        usherModeRecordDialog.cancel();
                                                        ToastTools.newInstance(TrainingAudioSettingActivity.this).showText(getString(R.string.upload_complete));
                                                        path=  strMsgBean.getResult().toString();
                                                        imageViewRecord.setVisibility(View.VISIBLE);
                                                        imageViewText.setVisibility(View.GONE);
                                                        imageViewAudio.setVisibility(View.GONE);
                                                        isTTS = true;
                                                }
                                        });
                                }
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure" + "http://" + IpMsgConst.SOCKET_HOST + ":7755/");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                usherModeRecordDialog.cancel();
                                                ToastTools.newInstance(TrainingAudioSettingActivity.this).showText(getString(R.string.network_request_failed));
                                        }
                                });
                        }
                });

        }


        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                super.onActivityResult(requestCode, resultCode, data);



                if (data == null) {
                        return;
                }
                if(resultCode == RESULT_OK){
                        if (requestCode == 101 ) {//音频
                                imageViewRecord.setVisibility(View.GONE);
                                imageViewText.setVisibility(View.GONE);
                                imageViewAudio.setVisibility(View.VISIBLE);
                                path = data.getStringExtra("mPicPath");
                                isTTS = true;
                                return;

                        }

                        if (requestCode == 102 ) {//文字
                                imageViewRecord.setVisibility(View.GONE);
                                imageViewAudio.setVisibility(View.GONE);
                                imageViewText.setVisibility(View.VISIBLE);
                                path = data.getStringExtra("ttsResult");
                                ttsText = path;
                                isTTS = false;
                                return;
                        }

                        if (requestCode == 111 ) {//动作Id
                                actionId = data.getStringExtra("actionId");
                        }


                }
        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
        }

}
